Accept decimal strings in Intl.NumberFormat #57595
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #52124
Note: This is not a coercion like in other JS APIs that also "accept" strings by coercing them, it is specifically designed to handle arbitrary-length decimals without precision loss. This was added as an explicit feature in Intl.NumberFormat v3 (which is why I added it to
es2023.intl
, which also housesformatRange()
, which also got added in v3): https://github.com/tc39/proposal-intl-numberformat-v3#interpret-strings-as-decimals-ecma-402-334The format matches what
${number}
accepts (plusInfinity
):Spec link: https://tc39.es/ecma402/#sec-runtime-semantics-stringintlmv
It is very common to represent monetary numbers as strings in JSON APIs to avoid any precision loss and those can currently not be passed to the
Intl.NumberFormat
API without castingas any
. The resulting type errors currently misleads developers into usingparseFloat()
to make the type error go away, which causes precision loss.It is supported by all browsers: https://caniuse.com/mdn-javascript_builtins_intl_numberformat_format_number_parameter-string_decimal